/******************************************************************
 * Created by: GOH CHEE HONG                                     
 * Date of Creation: 1/Sep/2008    
 * Date of last modification: 
 *                                                                
 * Description: VIEW DETAIL OF A PUBLIC KEY
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pbdms.Common.CryptoUtils;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.Encoders;

namespace Pbdms.Forms.Views
{
    public partial class FrmDetailKey : Form
    {
        private string key;
        public FrmDetailKey()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
        }
        public FrmDetailKey(string key)
        {
            this.key = key;
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
            load_info();
        }
        public string getKeyID()
        {
            return key;
        }

        public void setNewKey(string key)
        {
            this.key = key;
            load_info();
        }
        /// <summary>
        /// view detailed information about a public key
        /// </summary>
        private void load_info()
        {
            
            PgpPublicKey pubKey = Program.publicKeyControl.getPublicKey(key);
            int i = 0;
            foreach (string temp in pubKey.GetUserIds())
            {

                string[] info = temp.Split('>','<');
                
                
                txtName.Text = info[1];
                
                txtEmail.Text = info[3];
                if (info.Length == 7)
                {
                    txtComment.Text = info[5];
                }
                else
                {
                    txtComment.Text = "No Comments";
                }
             
                break;
            }
            lstSigns.Items.Clear();
            txtFinger.Text = Common.Global.convertFingerPrint(pubKey.GetFingerprint());
            txtKey.Text = pubKey.KeyId.ToString("X");
            Verifier verifier = new Verifier();
            PgpPublicKey myPubkey = Program.keyControl.readSecretKey().PublicKey;
            foreach (PgpSignature sign in pubKey.GetSignaturesOfType(PgpSignature.DirectKey))
            {
              
                PgpPublicKey pubKeyToBeCheck = Program.publicKeyControl.getPublicKey(sign.KeyId.ToString("X"));
                if (myPubkey.KeyId.ToString("X").Equals(sign.KeyId.ToString("X")))
                {
                    if (verifier.VerifySignature(sign, Program.keyControl.readSecretKey().PublicKey, pubKey.KeyId.ToString("X")))
                    {
                        lstSigns.Items.Add("Signature (" + sign.KeyId.ToString("X") + ") belong to you");
                    }
                }
                else if (pubKeyToBeCheck != null)
                {
                    if (verifier.VerifySignature(sign, pubKeyToBeCheck, pubKey.KeyId.ToString("X")))
                    {

                        string id = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(sign.KeyId.ToString("X"));
                        Common.DataObject.Buddy buddy= BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(id);
                        lstSigns.Items.Add("Signature (" + sign.KeyId.ToString("X") + ") belong to " + buddy.Name);
                    }

                }
                else
                {
                    lstSigns.Items.Add("Signature (" + sign.KeyId.ToString("X") + ") unknown");
                }
            }
        }

        private void btnCLose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}